package com.ny.mapper;

import com.ny.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
  // 原有方法...
  @Select("select * from user")
  List<User> findAll();

  @Select("select * from user where username=#{username}")
  User findUserByUsername(@Param("username") String username);

  @Select("select * from user where nickname=#{nickname} ")
  User findUserByNackName(@Param("nickname") String nickname);

  @Select("select count(*) from user where username=#{username}")
  int countByUsername(@Param("username") String username);

  @Insert("insert into user(username, password, nickname, phone, email, is_active, created_time) " +
          "values(#{username}, #{password}, #{nickname}, #{phone}, #{email}, #{is_active}, #{created_time})")
  int insertUser(User user);

  // ---------------- 新增以下方法 ----------------
  /**
   * 根据邮箱查询用户（用于验证邮箱是否已注册）
   */
  @Select("select * from user where email=#{email}")
  User findUserByEmail(@Param("email") String email);

  /**
   * 根据邮箱更新用户密码（密码重置核心方法）
   * @return 影响行数（1=成功，0=失败）
   */
  @Update("update user set password=#{newPassword} where email=#{email}")
  int updatePasswordByEmail(
          @Param("email") String email,
          @Param("newPassword") String newPassword
  );
}